<?php
class sales_finder_sales{

    var $column_detail = 'detail';
    function detail_edit($id){        
        
        #[发货配置]是否启动拆单 ExBOY
        $orderSplitLib    = kernel::single('ome_order_split');
        $split_seting     = $orderSplitLib->get_delivery_seting();

        $render = app::get('sales')->render();
        $Oorders = app::get('ome')->model('orders');

        $oItem = kernel::single("ome_mdl_sales_items");
        $sales = app::get('sales')->model('sales')->getList('*',array('sale_id'=>$id));
        $dataitems = $oItem->getList('*',array('sale_id'=>$id));
        $total_nums = $oItem->getList('sum(nums) as total_nums',array('sale_id'=>$id));
        $archive = $sales[0]['archive'];
        
        $order_id = $sales[0]['order_id'];
        if ($archive == '1') {
            $archive_delObj = kernel::single('archive_interface_delivery');
            $archive_ordObj = kernel::single('archive_interface_orders');
            $sql2 = 'select ODB.logi_no from sdb_archive_delivery_bill as ODB left join sdb_archive_delivery_order as ODO on ODB.delivery_id = ODO.delivery_id left join sdb_ome_sales as OS on OS.order_id= ODO.order_id where OS.sale_id= '.$sales[0]['sale_id'];
            $delivery_bill = $archive_delObj->getDeliveryByorderId($order_id);
           
            $orders =$archive_ordObj->getOrders($order_id,'order_bn');
            $orders[0] = $orders;
            $delivery = $archive_delObj->getDelivery(array('delivery_id'=>$sales[0]['delivery_id']),"delivery_bn,ship_name,ship_area,ship_province,ship_city,ship_district,ship_addr,ship_zip,ship_tel,ship_mobile,ship_email");
        }else{
            $sql2 = 'select ODB.logi_no from sdb_ome_delivery_bill as ODB left join sdb_ome_delivery_order as ODO on ODB.delivery_id = ODO.delivery_id left join sdb_ome_sales as OS on OS.order_id= ODO.order_id where OS.sale_id= '.$sales[0]['sale_id'];
            $delivery_bill = kernel::database()->select($sql2);
            $orders = $Oorders->getList('order_bn,is_service_order,service_price',array('order_id'=>$sales[0]['order_id']));
            $sql5 = "select delivery_bn,ship_name,ship_area,ship_province,ship_city,ship_district,ship_addr,ship_zip,ship_tel,ship_mobile,ship_email from sdb_ome_delivery where delivery_id='".$sales[0]['delivery_id']."'";
            $delivery = kernel::database()->select($sql5);
        }
        $sql3 = "select name from sdb_ome_shop where shop_id='".$sales[0]['shop_id']."'";
        $shopname = kernel::database()->select($sql3);

        $sql4 = "select uname from sdb_ome_members where member_id='".$sales[0]['member_id']."'";
        $uname = kernel::database()->select($sql4);
        
        //显示关联的销售物料编码,促销销售物料显示关联的基础物料信息
        if($dataitems)
        {
            $salesBasicMaterialObj    = app::get('material')->model('sales_basic_material');
            $orderItemObj             = app::get('ome')->model('order_items');
            $orderObjects             = app::get('ome')->model('order_objects');
            
            $material_sales_type      = array('product'=>'普通', 'pkg'=>'促销', 'gift'=>'赠品');
            
            foreach ($dataitems as $key => $val)
            {
                if(empty($val['product_id']))
                {
                    #促销销售物料信息
                    $filter     = array('order_id'=>$sales[0]['order_id'], 'bn'=>$val['bn'], 'obj_type'=>'pkg');
                    $getItem    = $orderObjects->dump($filter, 'obj_id, goods_id, obj_type');
                    $val['obj_type']    = $getItem['obj_type'];
                    $val['type_name']   = $material_sales_type[$getItem['obj_type']];
                    
                    #订单商品详细信息
                    $filter                = array('obj_id'=>$getItem['obj_id']);
                    $val['order_items']    = $orderItemObj->getList('item_id, product_id, bn, name, price, pmt_price, sale_price, amount, nums, `delete`', $filter);
                    
                    #优惠金额平摊
                    $items_count    =  count($val['order_items']);
                    if($val['apportion_pmt'])
                    {
                        #关联基础物料贡献占比
                        $salesBasicMList = array();
                        $tempData        = $salesBasicMaterialObj->getList('bm_id, rate', array('sm_id'=>$getItem['goods_id']));
                        foreach ($tempData as $sKey => $sVal)
                        {
                            $salesBasicMList[$sVal['bm_id']]    = $sVal['rate'];
                        }
                        
                        #平摊优惠
                        $bm_rate          = 0;
                        $apportion_pmt    = 0;
                        $pmt_price        = 0;
                        $item_i           = 0;
                        
                        foreach ($val['order_items'] as $iKey => $iVal)
                        {
                            $item_i++;
                            
                            if($item_i == $items_count)
                            {
                                $val['order_items'][$iKey]['apportion_pmt']    = $val['apportion_pmt'] - $apportion_pmt;
                                //$val['order_items'][$iKey]['pmt_price']        = $val['pmt_price'] - $pmt_price;
                                
                                //PKG销售金额 = 销售金额 - 平摊优惠
                                $val['order_items'][$iKey]['amount']    = $val['order_items'][$iKey]['amount'] - $val['order_items'][$iKey]['apportion_pmt'];
                            }
                            else
                            {
                                $bm_rate    = $salesBasicMList[$iVal['product_id']] / 100;
                                
                                $val['order_items'][$iKey]['apportion_pmt']    = round(($val['apportion_pmt'] * $bm_rate), 2);
                                //$val['order_items'][$iKey]['pmt_price']        = round(($val['pmt_price'] * $bm_rate), 2);
                                
                                //PKG销售金额 = 销售金额 - 平摊优惠
                                $val['order_items'][$iKey]['amount']    = $val['order_items'][$iKey]['amount'] - $val['order_items'][$iKey]['apportion_pmt'];
                                
                                $apportion_pmt  += $val['order_items'][$iKey]['apportion_pmt'];
                                //$pmt_price      += $val['order_items'][$iKey]['pmt_price'];
                            }
                        }
                    }
                }
                else 
                {
                    $filter     = array('order_id'=>$sales[0]['order_id'], 'product_id'=>$val['product_id'], 'item_type'=>array('product', 'gift'));
                    $getItem    = $orderItemObj->dump($filter, 'item_id, item_type');
                    $val['obj_type']    = $getItem['item_type'];
                    $val['type_name']   = $material_sales_type[$getItem['item_type']];
                }
                
                $dataitems[$key]    = $val;
            }
        }
        
        /*------------------------------------------------------ */
        //-- [拆单]获取订单对应多个发货单 ExBOY
        /*------------------------------------------------------ */
        if($split_seting)
        {
            $sql    = "SELECT dord.delivery_id, d.delivery_bn, d.logi_no FROM sdb_ome_delivery_order AS dord
                        LEFT JOIN sdb_ome_delivery AS d ON(dord.delivery_id=d.delivery_id)
                        WHERE dord.order_id='".$sales[0]['order_id']."' AND (d.parent_id=0 OR d.is_bind='true') AND d.disabled='false'
                        AND d.status NOT IN('failed','cancel','back','return_back')";
            $delivery_list    = kernel::database()->select($sql);
            
            #获取订单对应所有发货单
            if($delivery_list && count($delivery_list) > 1)
            {
                $delivery[0]['delivery_bn']    = '';
                $sales[0]['logi_no']           = '';
                
                foreach($delivery_list as $key => $val)
                {
                    $delivery[0]['delivery_bn']    .= ' | '.$val['delivery_bn'];
                    $sales[0]['logi_no']           .= ' | '.$val['logi_no'];
                }
                
                $delivery[0]['delivery_bn']    = substr($delivery[0]['delivery_bn'], 2);
                $sales[0]['logi_no']           = substr($sales[0]['logi_no'], 2);
            }
        }
        
        if($delivery_bill){
            foreach ($delivery_bill as $value){
                $sales[0]['logi_no'] .=' | '.$value['logi_no'];
            }
        }
        $sales[0]['order_bn'] = $orders[0]['order_bn'];
        $sales[0]['sale_time'] = date("Y-m-d H:i:s",$sales[0]['sale_time']);
        $sales[0]['delivery_bn'] = $delivery[0]['delivery_bn'];
        $sales[0]['shopname']  = $shopname[0]['name'];
        $sales[0]['uname']   = $uname[0]['uname'];
        $sales[0]['nums']   = $total_nums[0]['total_nums'];
        $sales[0]['is_service_order']   = $orders[0]['is_service_order'];
        $sales[0]['service_price']   = $orders[0]['service_price'];
        // 代销人信息
        $selling_agent_info = app::get('ome')->model('order_selling_agent')->dump($sales[0]['selling_agent_id']);

        $render->pagedata['selling_agent_info'] = $selling_agent_info;
        $render->pagedata['deliveryinfo'] = $delivery[0];
        $render->pagedata['dataitems'] = $dataitems;
        $render->pagedata['sales'] = $sales[0];
        $render->display('detail.html');
    }

    var $addon_cols = 'archive,order_id';
    var $column_order_id='订单号';
    var $column_order_id_width='100';
    function column_order_id($row)
    {
        $archive = $row[$this->col_prefix . 'archive'];
        
        $order_id = $row[$this->col_prefix . 'order_id'];
        if ($archive == '1') {
            $archive_ordObj = kernel::single('archive_interface_orders');
            $order =$archive_ordObj->getOrders($order_id,'order_bn');
            
        }else{
            $orderObj = app::get('ome')->model('orders');
            $filter = array('order_id'=>$order_id);
            $order = $orderObj->dump($filter,'order_bn');
        }
        

        return $order['order_bn'];
    }
}
